import junit.framework.TestCase;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;

/**
 * Created by GoldenMan on 2016/12/14.
 */
public class SpringTestCase extends TestCase {
    private static ClassPathXmlApplicationContext resource;
    private static BeanFactory factory;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        resource = new ClassPathXmlApplicationContext("spring-base.xml", "spring-dao.xml", "spring-service.xml", "spring-action.xml");
        factory = resource;

    }

    BeanFactory getFactory() {
        return factory;
    }

    public File getFileByClass(String filePath) {
        URL url = this.getClass().getResource(filePath);
        String path;
        try {
            path = URLDecoder.decode(url.getFile(), "GBK");

        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        File file = new File(path);
        if (!file.exists()) {
            throw new RuntimeException("文件不存在！");
        }
        return file;
    }
}
